package com.example.myone.Pager;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Adapter.DiscoverArticleAdapter;
import com.example.myone.Base.BasePager;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.ArticleCategoryEnum;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.R;
import com.example.myone.Util.DateDialogUtil;
import com.example.myone.Util.DateTimeUtil;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@SuppressLint("NewApi")
public class DiscoverPager extends BasePager implements DateDialogUtil.DateSelectedListener {

    private TabLayout tabLayout;
    private RecyclerView recyclerView;
    private DiscoverArticleAdapter adapter;
    private ImageView datePicker;
    private TextView dateText;
    private String selectedCategory;
    private String selectedDate;

    public DiscoverPager(Context context) {
        super(context);
    }

    @Override
    public void onDateSelected(int year, int month) {
        selectedDate = String.format(Locale.getDefault(), "%04d-%02d", year, month);
        dateText.setText(selectedDate);
        adapter.updateData(ArticleMapper.getArticleByCategoryAndTime(context, selectedCategory, selectedDate));
    }

    @Override
    public View initView() {
        Log.d(TAG, "==> DiscoverPager initView");
        // 加载布局文件
        View view = LayoutInflater.from(context).inflate(R.layout.discover_pager, null);
        dateText = view.findViewById(R.id.tv_date);
        datePicker = view.findViewById(R.id.btn_date_picker);
        // 日期选择
        datePicker.setOnClickListener(v -> {
            DateDialogUtil.showYearMonthPicker(context, this);
        });
        tabLayout = view.findViewById(R.id.tabLayout);
        recyclerView = view.findViewById(R.id.recyclerView);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.d(TAG, "==> DiscoverPager initData");
        // 初始化导航栏
        List<String> tabTitles = ArticleCategoryEnum.getAllCategory();
        for (String title : tabTitles) {
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        List<Article> initialData =
                ArticleMapper.getArticleByCategoryAndTime(context,
                        ArticleCategoryEnum.DAILY.getMessage(),
                        DateTimeUtil.getNowYearMonthStr()
                );
        adapter = new DiscoverArticleAdapter(context, initialData);
        recyclerView.setAdapter(adapter);
        selectedDate = DateTimeUtil.getNowYearMonthStr();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String tabText = (String) tab.getText();
                selectedCategory = tabText;
                List<Article> newData = new ArrayList<>();
                if (tabText != null && !tabText.isEmpty()) {
                    newData = ArticleMapper.getArticleByCategoryAndTime(context, selectedCategory, selectedDate);
                }
                adapter.updateData(newData);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


}

